﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sharp.DataTransferObject;
using Sharp.DataTransferObject.Query;
using Sharp.Domain.PlatformBoundedContext.PermissionModule.Aggregates;
using Sharp.Infrastructure;
using Sharp.Infrastructure.IoC;

namespace Sharp.Application.Platform
{
    public partial class UserServiceImpl
    {
        /// <summary>
        ///     全部权限
        /// </summary>
        /// <returns></returns>
        public Task<IEnumerable<PermissionDTO>> AllPermissions()
        {
            return Task.Run(() =>
            {
                return _permissionRepository.GetList(x => true, "SystemCode ASC,OrderIndex ASC", null, (string)null).To<Permission, PermissionDTO>();
            });
        }

        /// <summary>
        ///     全部可用权限
        /// </summary>
        /// <returns></returns>
        public Task<IEnumerable<PermissionDTO>> AllAvailablePermissions()
        {
            return Task.Run(() =>
            {
                var availablePermissionRepository =
                    ServiceLocator.Instance.GetService<IAvailablePermissionRepository>();
                return availablePermissionRepository.GetList(x => true, "SystemCode ASC,OrderIndex ASC", null, (string) null)
                    .To<AvailablePermission, PermissionDTO>();
            });
        }

        /// <summary>
        ///     保存权限
        /// </summary>
        /// <param name="ids">被勾选的权限列表</param>
        /// <param name="owner">所有者</param>
        /// <param name="ownerId">所有者id</param>
        /// <remarks>通过所有者来判断具体是给用户或者角色进行权限配置</remarks>
        /// <returns></returns>
        public Result SavePermission(IList<Guid> ids, string owner, int ownerId)
        {
            return _domainService.SavePermission(ids, owner, ownerId);
        }


        /// <summary>
        ///     获取用户权限
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public Task<IEnumerable<PermissionDTO>> GetPermissionById(int uid)
        {
            if (uid.IsEmpty())
            {
                throw new NullReferenceException("参数id不能为空。");
            }
            return Task.Run(() =>
            {
                return _domainService.GetPermissionById(uid).To<Permission, PermissionDTO>();
            });
        }

        /// <summary>
        /// 更新权限库
        /// </summary>
        /// <param name="dtos"></param>
        /// <param name="columns"></param>
        /// <returns></returns>
        /// <remarks>初始化系统的用的功能</remarks>
        public Result RefurbishAvailablePermissionData(PermissionDataQuery query)
        {

            return _domainService.RefurbishAvailablePermissionData(query.Dtos.To<PermissionDTO, AvailablePermission>()
                .ToList(), query.Columns);
        }

        /// <summary>
        /// 分配所有权限给超级管理员角色
        /// </summary>
        /// <returns></returns>
        /// <remarks>初始化系统的用的功能</remarks>
        public Result AllotAllPermissionToAdminRole()
        {
            return _domainService.AllotAllPermissionToAdminRole();
        }

        /// <summary>
        /// 保存可用权限到权限列表
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public Result AvailablePermissionToPermission(Guid[] ids)
        {
            var res = _domainService.AvailablePermissionToPermission(ids);
            return res;
        }

        /// <summary>
        /// 保存组织结构数据权限
        /// </summary>
        /// <returns></returns>
        public Result SaveOrgPermission(Guid[] orgIds, int roleId)
        {
            var res = _domainService.SaveOrgPermission(orgIds, roleId);

            return res;
        }
    }
}